package com.example.distillapplication.ui.fragment.login;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.distillapplication.R;
import com.example.distillapplication.data.entity.UserInfo;
import com.example.distillapplication.databinding.FragmentRegisterBinding;
import com.example.distillapplication.ui.fragment.base.BaseFragment;
import com.example.distillapplication.ui.fragment.viewpager.RegisterListener;

import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class RegisterFragment extends BaseFragment {
    FragmentRegisterBinding fragmentRegisterBinding;
    RegisterListener listener;

    public RegisterFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        listener = (RegisterListener) mActivity;
        View view = inflater.inflate(R.layout.fragment_register, container, false);
        fragmentRegisterBinding = FragmentRegisterBinding.bind(view);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        fragmentRegisterBinding.register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //一些逻辑
                String password1 = fragmentRegisterBinding.password.getText().toString();
                String password2 = fragmentRegisterBinding.password2.getText().toString();
                String account = fragmentRegisterBinding.account.getText().toString();
                List<UserInfo> accounts = mDataBaseViewModel.dataBaseRepository.getUserInfoByAccount(account);
                if(!password1.equals(password2)){
                    showLongToast("2次输入的密码应该一致！");
                }else if(accounts.size()>0){
                    showLongToast("账号已经存在！");
                }else{
                    mDataBaseViewModel.dataBaseRepository.insertUserInfo(new UserInfo(fragmentRegisterBinding.name.getText().toString(),
                            account, password1, fragmentRegisterBinding.comName.getText().toString(), fragmentRegisterBinding.address.getText().toString()));
                    listener.returnLoginFragment();
                }


            }
        });
    }
}
